/*
 * Copyright © 2011 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.classes;

import javax.microedition.lcdui.*;

/**
 *
 * @author jcccn
 * @site http://www.senseforce.com/
 */
 
 /*
 *通过定时器实现
 */
 import java.util.*;
 public class BouncingBallCanvas extends Canvas {
 
	private static final int ballDiameterTop = 20;		//在最上方时的直径
	private static final int ballDiameterBottom = 12;	//在最下面时的直径
	private static final int stepY = 2;
	private boolean isDown = true;	//小球是否向下运动
	private int[] drawRGB = {0, 255, 0};	//小球边缘颜色
	private int[] fillRGB = {128, 128, 128};	//小球的填充颜色
	private int[] ballShape = {0, ballDiameterTop/2, ballDiameterTop, ballDiameterTop};	//{中心x,中心y,宽度w,高度h}
	private int ballPosYMax = 0;
	private int ballPosYMin = 0;
	
	private Timer timer = null;
	private BouncingTask timerTask = null;
	
	public BouncingBallCanvas() {
		ballShape[0] = getWidth() / 2 - ballDiameterTop / 2;
        ballShape[1] = 0;
		ballPosYMin = 0;
		ballPosYMax = getHeight() - 20;
		
		timer = new Timer();
		timerTask = new BouncingTask();
		timer.schedule(timerTask, 0, 10);
	}
	
	public void paint(Graphics g) {
		//setFullScreenMode(true);
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(fillRGB[0], fillRGB[1], fillRGB[2]);
		g.fillArc(ballShape[0], ballShape[1], ballShape[2], ballShape[3], 0, 360);
		g.setColor(drawRGB[0], drawRGB[1], drawRGB[2]);
		g.drawArc(ballShape[0], ballShape[1], ballShape[2], ballShape[3], 0, 360);
	}
	
	public void pauseBouncing() {
		timer.cancel();
		timer = null;
		timerTask = null;
	}
	
	public void resumeBouncing() {
		timer = new Timer();
		timerTask = new BouncingTask();
		timer.schedule(timerTask, 0, 10);
	}
	
	class BouncingTask extends TimerTask {
		public void run() {
			repaint();
			if (isDown) {	//向下
				if (ballShape[1] < ballPosYMax) {
					ballShape[1] += stepY;
				}
				else {
					ballShape[1] -= stepY;
					isDown = false;
				}
			}
			else {			//向上
				if (ballShape[1] > ballPosYMin) {
					ballShape[1] -= stepY;
				}
				else {
					ballShape[1] += stepY;
					isDown = true;
				}
			}
		}
	}
 }
 
 /*
 * 通过多线程方式实现
 
public class BouncingBallCanvas extends Canvas implements Runnable {

	private static final int ballDiameterTop = 20;		//在最上方时的直径
	private static final int ballDiameterBottom = 12;	//在最下面时的直径
	private boolean isRunning = true;
	private static final int stepY = 2;
	private boolean isDown = true;	//小球是否向下运动
	private int[] drawRGB = {0, 255, 0};	//小球边缘颜色
	private int[] fillRGB = {128, 128, 128};	//小球的填充颜色
	private int[] ballShape = {0, ballDiameterTop/2, ballDiameterTop, ballDiameterTop};	//{中心x,中心y,宽度w,高度h}
	//private int ballPosX = 0;
	//private int ballPosY = 0;
	private int ballPosYMax = 0;
	private int ballPosYMin = 0;
	
	private Thread thread = null;

	public BouncingBallCanvas() {
//		ballShape[0] = getWidth() / 2;
//		ballShape[1] = ballDiameterTop / 2;
        ballShape[0] = getWidth() / 2 - ballDiameterTop / 2;
        ballShape[1] = 0;
		ballPosYMin = 0;
		ballPosYMax = getHeight() - 20;
		
		thread = new Thread(this);
		thread.start();
		
	}
	
	public void paint(Graphics g) {
		//setFullScreenMode(true);
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(fillRGB[0], fillRGB[1], fillRGB[2]);
		g.fillArc(ballShape[0], ballShape[1], ballShape[2], ballShape[3], 0, 360);
		g.setColor(drawRGB[0], drawRGB[1], drawRGB[2]);
		g.drawArc(ballShape[0], ballShape[1], ballShape[2], ballShape[3], 0, 360);
	}
	
	public void run() {
		while (isRunning) {
			repaint();
			try {
				Thread.sleep(10);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
			
			if (isDown) {	//向下
				if (ballShape[1] < ballPosYMax) {
					ballShape[1] += stepY;
				}
				else {
					ballShape[1] -= stepY;
					isDown = false;
				}
			}
			else {			//向上
				if (ballShape[1] > ballPosYMin) {
					ballShape[1] -= stepY;
				}
				else {
					ballShape[1] += stepY;
					isDown = true;
				}
			}
			
		}
	}
	
	public void pauseBouncing() {
		isRunning = false;
		thread = null;
	}
	
	public void resumeBouncing() {
		isRunning = true;
		thread = new Thread(this);
		thread.start();
	}
}
*/
